package pers.myutil.local.rank;


import pers.myutil.throwable.local.MyUtilLocalException;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

public class MapRank {

    /**
     * 使用 Map按key进行排序
     * @param map 需要排序的Map
     * @return 排序后的Map
     */
    public static Map<String, Object> sortMapByKey(Map<String, Object> map) throws MyUtilLocalException {
        if (map == null || map.isEmpty()) {
            throw new MyUtilLocalException("需要排序的Map不能为空！");
        }
        Map<String, Object> sortMap = new TreeMap<String, Object>(
                new MapKeyComparator());
        sortMap.putAll(map);
        return sortMap;
    }

}

class MapKeyComparator implements Comparator<String> {
    @Override
    public int compare(String str1, String str2) {
        return str1.compareTo(str2);
    }
}

